package route

import (
	"chatgpt/controller"
	"chatgpt/controller/app"
	"chatgpt/controller/middleware"
	"chatgpt/helper"
	"github.com/gin-gonic/gin"
)

func Run() {
	router := gin.Default()
	router.Use(middleware.Cors())
	register(router)
	appRouter(router)
	glmRouter(router)
	staticRouter(router)
	backendRouter(router)
	vp := helper.Config().Sub("Server").Sub("http")
	router.Run(vp.GetString("addr"))
}

func register(router *gin.Engine) {
	//router.Any("/system/login", controller.HandleLogin)

	//system := router.Group("/system", middleware.MustLogin())
	apir := router.Group("/question", middleware.ApiKeyCheck())
	//apir := router.Group("/question")

	apir.Any("/quick", controller.QuestionHandle)
	apir.Any("/origin", controller.OriginHandle)
	image := router.Group("/image", middleware.ApiKeyCheck())
	image.Any("/generate", controller.ImageGenerateHandle)

	v1 := router.Group("/v1", middleware.ApiKeyCheck())
	//v1 := router.Group("/v1")

	//v1.Use(middleware.CheckBenefit()).Any("/completion", controller.CompletionHandle)
	//v1.Use(middleware.CheckBenefit()).Any("/chat", controller.ChatHandle)
	//v1.Use(middleware.CheckBenefit()).Any("/image-gen", controller.ImageGenerateHandle)

	v1.Any("/completion", controller.CompletionHandle)
	v1.Any("/chat", controller.ChatHandle)
	v1.Any("/image-gen", controller.ImageGenerateHandle)

	fine_tune := v1.Group("/fine-tunes")
	fine_tune.Any("/list", controller.FineTuneListHandle)

}

func appRouter(router *gin.Engine) {
	appr := router.Group("/app", middleware.RawData())

	userRouter(router)
	orderRouter(router)
	payRouter(router)
	productRouter(router)

	// common direct

	//scopeapi := appr.Group("", middleware.CheckBenefit())
	scopeapi := appr.Group("", middleware.MustLogin(), middleware.CheckBenefit())
	{
		scopeapi.Any("/quick", app.QuickHandle)
		scopeapi.Any("/completion", app.CompletionHandle)
		scopeapi.Any("/chat", app.ChatHandle)
	}

	appr.Any("/completion-clear", app.CompletionClearHandle)

	appr.Any("/record_list", app.GetRecordList) // 查看chatgpt统计数据
	appr.Any("/wx_openid", app.GetUserWxOpenID) // 获取用户微信的openid

	appr.Any("/comment", app.CommentList)
	// fine-tune
	fine_tune := appr.Group("/fine-tunes")
	fine_tune.Any("/list", app.FineTuneListHandle)
}

func userRouter(router *gin.Engine) {
	user := router.Group("/user", middleware.RawData())

	// 不需要 登陆验证的接口
	// 账号的方式直接 注册
	user.Any("account/register", app.RegisterWithAccountHandle)
	user.Any("account/login", app.LoginWithAccountHandle)

	// 手机方式注册
	user.Any("cell/register", app.RegisterWithCellHandle)
	user.Any("cell/login", app.LoginWithCellHandle)
	user.Any("cell/code", app.CellGetVerifyCode)

	// 需要登陆验证的接口
	user.Use(middleware.MustLogin())
	{
		user.Any("token/check", app.CheckToken)
		user.Any("update", app.UpdateHandle)
		user.GET("mine-center", app.MineCenterHandle)
	}
}

func orderRouter(router *gin.Engine) {
	order := router.Group("/app/order", middleware.RawData(), middleware.MustLogin())

	// 需要登陆验证的接口
	order.Any("mine-list", app.MineOrderListHandle)
	order.Any("show", app.OrderShowHandle)
	order.Any("firm", app.OrderFirmHandle)

}

func payRouter(router *gin.Engine) {
	pay := router.Group("/app/pay", middleware.RawData())

	// 不需要 登陆验证的接口
	//pay.Any("firm", app.PayHandle)

	// 需要登陆验证的接口
	pay.Use(middleware.MustLogin())
	pay.Any("firm", app.PayHandle)
	pay.Any("cancel", app.CancelHandle)
	pay.Any("recharge-list", app.PayRechargeListHandle)
	pay.Any("cost-list", app.CostListHandle)
}

func staticRouter(router *gin.Engine) {
	router.Static("/web", "./web")
	//router.StaticFS("/static", http.Dir("static"))
}

func productRouter(router *gin.Engine) {
	product := router.Group("/app/product", middleware.RawData())

	// 需要登陆验证的接口
	product.Use(middleware.MustLogin())
	product.Any("list", app.ProductListHandle)
	product.Any("show", app.ProductShowHandle)
}
